<template>
	<view>
		<van-card 
			:thumb="item.goods_small_logo || defaultPic" 
			v-for="item in goodsList" 
			:key="item.goods_id"
			:price="item.goods_price|toFixed" 
			:title="item.goods_name"
			:thumb-link="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"
		 />

	</view>
</template>

<script>
	import { getGoodsList } from '../../api/goods_list'
	import toast from '../../utils/toast.js'
	export default {
		data() {
			return {
				queryObj: {
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				total: 0,
				goodsList: [],
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				loading: false
			}
		},
		
		onLoad(options) {
			// 将页面参数转存到 this.queryObj 对象中
			// this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		
		methods: {
			async getGoodsList() {
				try{
					this.loading = true
					uni.showLoading({
						title:'数据加载中...'
					})
					
					const {message} = await getGoodsList(this.queryObj)
					// console.log(res);
					this.goodsList = [...this.goodsList, ...message.goods]
					this.total = message.total
				}finally{
					//TODO handle the exception
					this.loading = false
					uni.hideLoading()
				}
			},
			
			onReachBottom() {
				console.log("触底了");
				if (this.loading) {
					return	
				}
				this.queryObj.pagenum ++
				if (this.total > this.goodsList.length) {
					this.getGoodsList()
				} else {
					toast({
						title: '数据到底了'
					})
				}
				// this.getGoodsList()
			},
			
			async onPullDownRefresh() {
				this.queryObj.pagenum = 1
				this.goodsList = []
				this.loading = false
				await this.getGoodsList()
				
				uni.stopPullDownRefresh()
			}
		}
	}
</script>

<style>

</style>
